from pyecharts.charts import Line
from pyecharts.options import TitleOpts,LabelOpts
import json
# # 得到折线图对象
# line = Line()
# # 添加x轴数据
# line.add_xaxis(["中国","美国","英国"])
# # 添加y轴数据
# line.add_yaxis("GDP",[30,20,10])
# # 生成图表
# line.render();
f_us = open("O:\学Python\资料\第1-12章资料\第1-12章资料\资料\可视化案例数据\折线图数据\美国.txt","r",encoding="UTF-8")
us_data  =  f_us.read()
f_jp = open("O:\学Python\资料\第1-12章资料\第1-12章资料\资料\可视化案例数据\折线图数据\日本.txt","r",encoding="UTF-8")
jp_data  =  f_jp.read()
f_yd = open("O:\学Python\资料\第1-12章资料\第1-12章资料\资料\可视化案例数据\折线图数据\印度.txt","r",encoding="UTF-8")
yd_data  =  f_yd.read()


us_data = us_data.replace("jsonp_1629344292311_69436(","")
jp_data = jp_data.replace("jsonp_1629350871167_29498(","")
yd_data = yd_data.replace("jsonp_1629350745930_63180(","")
us_data = us_data[:-2]
jp_data = jp_data[:-2]
yd_data = yd_data[:-2]

us_dict = json.loads(us_data)
jp_dict = json.loads(jp_data)
yd_dict = json.loads(yd_data)

us_trend_data = us_dict['data'][0]['trend']
jp_trend_data = jp_dict['data'][0]['trend']
yd_trend_data = yd_dict['data'][0]['trend']
# print(trend_data)
us_x_data = us_trend_data['updateDate'][:314]
jp_x_data = jp_trend_data['updateDate'][:314]
yd_x_data = yd_trend_data['updateDate'][:314]
# print(us_x_data)
us_y_data = us_trend_data['list'][0]['data'][:314]
jp_y_data = jp_trend_data['list'][0]['data'][:314]
yd_y_data = yd_trend_data['list'][0]['data'][:314]
# print(us_y_data)

line = Line()
line.add_xaxis(us_x_data)
line.add_yaxis("美国确诊人数",us_y_data,label_opts=LabelOpts(is_show=False))
line.add_yaxis("日本确诊人数",jp_y_data,label_opts=LabelOpts(is_show=False))
line.add_yaxis("印度确诊人数",yd_y_data,label_opts=LabelOpts(is_show=False))
line.set_global_opts(
    title_opts=TitleOpts(title="2020年美日印三国确诊人数对比折现图",pos_left="center",pos_bottom="1%")
)
line.render()

f_us.close()
f_jp.close()
f_yd.close()